#include <stdio.h>
#include <stdlib.h>
#include "BasicMatic.h"

#define HIGHB(n)			(0xFFFFFFFF << (32 - n))
#define LOWB(n)				(0xFFFFFFFF >> (32 - n))
#define u32				unsigned int

unsigned int FindNextOf(char* str,const char ch,unsigned int from)
{
	unsigned int strl = strlength(str);
	for(;from < strl ; from++)
	{
		if (str[from] == ch) return from;

	}
	return 0;
}
unsigned int FindLastOf(char* str,const char ch,unsigned int from)
{
	for(;from > 0 ; from--)
	{
		if (str[from] == ch) return from;

	}
			if (str[0] == ch) return 0;
	return strlength(str);
}

template<typename A,typename B>
inline void _Memcpy(A from,B to, unsigned int size)
{
	return _Memcpy((const char* from),(char*)to,size);
}

void _Memcpy(const char* from,char* to,unsigned int size)//BIG ENDIAN & SMALL ENDIAN //need rewrite if not align
{
/*	unsigned int i = 0;
	if((unsigned int)
	if(size > 4){
	for(i = 0 ; i < size - 4 ;  i+=4)
	{
		*((unsigned int*)(to + i)) = *((unsigned int*)(from + i));
	}
	}
	if(size % 4 == 0) *((unsigned int*)(to + i)) = *((unsigned int*)(from + i));
	else
	{
		unsigned char remain = (size % 4) * 8;
		*(((unsigned int*)to + i)) &= ((~0) << remain);

		*(((unsigned int*)to + i)) |= ((*((unsigned int*)(from + i))) & (~(0xFFFFFFFF << remain)));
	}*/
//Not Align
	if(((u32)to) % 4 != ((u32)from) %4)
	{
		unsigned int i = 0 ;
		for(;i < size ; i ++)
		{
			to[i] = from[i];
		}
	}
	else
	{
		unsigned int i = 0;
		if(size < 4)
		{
			while(i < size)
			{
				to[i] = from[i];
				i++;
			}
			return; 
			
		}
		while((i < size) && (((((u32)to) + i) % 4) != 0))
		{
			to[i] = from[i];
			i++;
		}
		for(; i < size - 4 ;  i+=4)
		{
			*((unsigned int*)(to + i)) = *((unsigned int*)(from + i));
		}
		while(i < size)
		{
			to[i] = from[i];
			i++;
		}
	}

}

unsigned int strlength(const char* str) //BIG Endian and SMALL ENDIAN
{
	unsigned int i = 0;
	while(str[i] != 0)
	{
/*		unsigned int buff = *((unsigned int*)str + i);
		if((buff & LOWB(8))  == 0) return i;
		if((buff & (LOWB(8) << 8))  == 0) return i+1;
		if((buff & (LOWB(8) << 16))  == 0) return i+2;
		if((buff & (LOWB(8) << 24))  == 0) return i+3;
		i +=4;*/
		i++;
		
	}
	return i;
}

unsigned char StrCompare(char* a,char* b)
{
	unsigned int x = strlength(a);
	unsigned int y = strlength(b);
	if (x != y) return 0;
	unsigned int i = 0 ;
	for(; i < x ; i+=1)
	{
		if(a[i] != b[i]) return 0;
	}
	return 1;
}

unsigned char InsertStr(char* to,const char* from,unsigned int pos/*,unsigned int fromposa,unsigned int fromposb*/)//many problem
{
	char buff[256];
	_Memcpy(to + pos,buff,strlength(to) - pos + 1 );


	_Memcpy(from, to + pos,strlength(from));

	_Memcpy(buff, to + strlength(to), strlength(buff));

    return 0;
}

unsigned char AddStr(char* to,const char* from)
{
        unsigned int len = strlength(to);
	char* pt = (char*)((unsigned int)to + len);
	len = strlength(from);
	_Memcpy(from,pt,len);
	return len;
}

unsigned char CopyStr(char* to,const char* from)
{
    _Memcpy(from,to,strlength(from) + 1);
    return 1;
}

void GetStr(const char* from,char** to,unsigned int fpos,unsigned int spos)
{

}
unsigned char _MemAll(const char from,char* to,unsigned int size)
{
		unsigned int i = 0;
		if(size < 4)
		{
			while(i < size)
			{
				to[i] = from;
				i++;
			}
			return from; 
			
		}
		while((i < size) && (((((u32)to) + i) % 4) != 0))
		{
			to[i] = from;
			i++;
		}
		unsigned int mir = (from) | (from << 8) | (from << 16) | (from << 24);
		for(; i < size - 4 ;  i+=4)
		{
			*((unsigned int*)(to + i)) = mir;
		}
		while(i < size)
		{
			to[i] = from;
			i++;
		}
}
